package com.xuzz.study.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @author xuzz
 * @description
 * @date 2019-12-04 10:45
 */
@Component
public class TransactionUtils {

    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;

    public TransactionStatus begin()
    {
        return begin(new DefaultTransactionDefinition());
    }
    public TransactionStatus begin(TransactionDefinition transactionDefinition)
    {
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        return transactionStatus;
    }

    public void commit(TransactionStatus transactionStatus)
    {
        dataSourceTransactionManager.commit(transactionStatus);
    }
    public void rollback(TransactionStatus transactionStatus)
    {
        dataSourceTransactionManager.rollback(transactionStatus);
    }
}
